<!--
 * @Author: lp
 * @Date: 2022-01-20 11:44:19
 * @LastEditTime: 2022-04-15 14:49:24
 * @LastEditors: Please set LastEditors
 * @Description: 系统svg图片选择
 * @FilePath: \nien-vue3-admin\src\components\IconSelect\index.vue
-->
<template>
  <div class="icon-body">
    <el-input
      v-model="name"
      style="position: relative;"
      clearable
      placeholder="请输入图标名称"
      :prefix-icon="Search"
      @clear="filterIcons"
      @input="filterIcons"
    >
    </el-input>
    <el-scrollbar class="icon-list">
      <div
        v-for="(item, index) in iconList"
        :key="index"
        class="icon-item"
        @click="selectedIcon(item)"
      >
        <svg-icon :name="item" style="height: 0.13rem; width: 0.2rem;"></svg-icon>
        <span style="height: 0.16rem; width: 0.75rem;">{{ item }}</span>
      </div>
    </el-scrollbar>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import icons from './requireIcons'

const name = ref('')
const iconList = ref(icons)
const emit = defineEmits(['selected'])
const filterIcons = () => {
  iconList.value = icons
  if (name.value) {
    iconList.value = icons.filter((item: string) => item.includes(name.value))
  }
}
const selectedIcon = (name: string) => {
  emit('selected', name)
  document.body.click()
}
</script>

<style lang="less" scoped>
.icon-body {
  width: 100%;
  .icon-list {
    height: 200px;
    div {
      height: 30px;
      line-height: 30px;
      margin-bottom: -5px;
      cursor: pointer;
      width: 33%;
      float: left;
    }
    span {
      display: inline-block;
      vertical-align: -0.15em;
      fill: currentcolor;
      overflow: hidden;
    }
    .icon-item {
      width: 1rem;
      height: 0.3rem;
    }
  }
}
</style>
